package com.android.sms_manager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.android.sms_manager.data.*;
import com.android.sms_manager.service.*;

import java.util.List;
import java.util.Map;

import com.android.sms_manager.R;

public class MainActivity extends Activity{

	private List<Map<String,Object>>data = null;
	private String uri = null;
	private int protocol;
	private String title = null;
	private String query = null;
	private int type;
	private ListView conversationsList;
	private SearchView	searchv;
	private static int scrollY = -1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		title = this.getString(R.string.app_name);
		this.setTitle(title);
		
		searchv = (SearchView) this.findViewById(R.id.search_view1);				
		conversationsList = (ListView) this.findViewById(R.id.entrylist);
		
		this.registerForContextMenu(conversationsList);
		
		registerListener();

	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//when the activity is recalled, refrash the data, but it'll be slower.
			
		if(query != null){
			search(query);
		}
		else{
			loadData();
		}
		if(scrollY!=-1)		
			conversationsList.setSelection(scrollY);
		
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		scrollY = conversationsList.getFirstVisiblePosition();
	}
		
	
	
	//Do click event 
	private void registerListener(){
		conversationsList.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(MainActivity.this,ListSmsActivity.class);
				intent.putExtra(MyData.KEY_URI, uri);
				intent.putExtra(MyData.KEY_TITLE, title+">"
						+data.get(position).get(MyData.KEY_NAME).toString());
				intent.putExtra(MyData.KEY_PROTOCOL, protocol);
				intent.putExtra(MyData.KEY_THREAD_ID,
						(Integer)(data.get(position).get(MyData.KEY_THREAD_ID)));
				intent.putExtra(MyData.KEY_MSG_BOX, type);
				intent.putExtra(MyData.KEY_ADDRESS, 
						data.get(position).get(MyData.KEY_ADDRESS).toString());
				
				if(query != null)
					intent.putExtra(MyData.KEY_ID, 
						(Integer)(data.get(position).get(MyData.KEY_ID)));
				
				startActivity(intent);
				
			}});	
		
		searchv.setOnCloseListener(new OnCloseListener(){

			@Override
			public boolean onClose() {
				// TODO Auto-generated method stub
				query = null;
				loadData();
				return false;
			}
			
		});
		searchv.setOnQueryTextListener(new OnQueryTextListener() {         
  
            @Override  
            public boolean onQueryTextSubmit(String queryText) {  
                if (searchv != null) {             	               	
                	query = queryText;
                	search(query);            	
                	searchv.clearFocus(); 
                }
                return true;
            }    

			@Override
			public boolean onQueryTextChange(String arg0) {
				// TODO Auto-generated method stub
				return false;
			}  
          
        });  
	}  
	
	//load all data.
	private void loadData(){
		uri = MyData.URI_SMS_ALL;
		protocol = MyData.PROTOCOL_SMS;
		String from[] = null;
		
		data = QueryService.getConversation(this, uri, protocol,null);
		
		from = new String[]{MyData.KEY_NAME,
				MyData.KEY_MSG_COUNT,
				MyData.KEY_DATE,
				MyData.KEY_BODY,
				MyData.KEY_ID};
		
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.conversationitem,
				from, new int[]{R.id.conversationname,
						R.id.conversationmsgcount,
						R.id.conversationdate,
						R.id.conversationsnippet});
		
		if(data !=null && !data.isEmpty())
			conversationsList.setAdapter(adapter);
	}
	
	//load data of the search result
	private void search(String querytext){
		uri = MyData.URI_SMS_ALL;
		protocol = MyData.PROTOCOL_SMS;
		String from[] = null;
		//get result by the query content.
		data = QueryService.getConversation(this, uri, protocol,querytext);
		if(data !=null && !data.isEmpty()){
			
			from = new String[]{MyData.KEY_NAME,
								MyData.KEY_DATE,
								MyData.KEY_BODY};
		
			SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.conversationitem,
					from, new int[]{R.id.conversationname,
									R.id.conversationdate,
									R.id.conversationsnippet});		
			conversationsList.setAdapter(adapter);
		}
		else{
			conversationsList.setAdapter(null);
			Toast.makeText(MainActivity.this, R.string.noresult, Toast.LENGTH_LONG).show();
		}
	}
	
	private static final int MENU_DRAFT = 1;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, MENU_DRAFT, 0, R.string.draft);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		switch(item.getItemId()){
		case MENU_DRAFT:
			//*************************************************
			Intent intent = new Intent(MainActivity.this,ListDraftActivity.class);
			startActivity(intent);
			//************************************************
			break;
		default:
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}
}
